package geowiki;

import geowiki.data.Place;
import geowiki.data.Link;
import geowiki.data.PlacemakerPlaceExtractor;
import geowiki.data.YQLLinkExtractor;
import geowiki.data.YQLQuery;
import geowiki.utils.WikipediaUtils;

import java.util.ArrayList;
import java.util.List;

import processing.core.PApplet;

public class MultipleContentGeoWikiApp extends GeoWikiApp {

List<Link> links;


	public void setup() {
		super.setup();
		
		List<Place> places = loadPlacesViaPlacemakerFromWiki("Passau");
		placeMarkers = createPlaceMarkers(places);


             //** this works but will take ages to complete!!

             //   List<Link> links = loadLinks("Passau");
             //   List<List<Place>> linkedplaces = loadLinkedPlaces("Passau");

	}



         ///** Will extract the LinkedPlaceloader soon into extra class for threading

        public List<List<Place>> loadLinkedPlaces(String wikipediaArticleName){

          links = loadLinks(wikipediaArticleName);
          List<List<Place>> linkedPlaces = new ArrayList<List<Place>>();

          for (int i = 0; i < links.size(); i++) {

			println(links.get(i) + " /" + i + " of " + links.size());
			String linkURL = WikipediaUtils.getFullURLFromLink(links.get(i).url);
                        println("fullURL:"+linkURL);

                        //String restURL = YQLQuery.getAllPlacesFromWebpage(linkURL);

			List<Place> placesOfLink = loadPlacesViaPlacemakerFromURL(linkURL);
			println("PlacesFound:" + placesOfLink);

                        linkedPlaces.add(i, placesOfLink);

                        links.get(i).storePlaceMarkers(linkedPlaces.get(i));
                        
                        //linkedPlaceMarkers = createPlaceMarkers(linkedPlaces.get(i));
			// createLinkedPlaceMarkers(linkedPlaces.get(i), (int) random(255), (int) random(255), (int) random(255));
			// createLinkedPlaceMarkers(linkedPlaces.get(i), 0, (int) random(255), (int) random(255));
		}

          return linkedPlaces;
        }

        public List<Link> loadLinks(String wikipediaArticleName) {

             YQLLinkExtractor linkExtractor = new YQLLinkExtractor(this);
             String wikipediaURL = WikipediaUtils.getWikipediaArticleURL(wikipediaArticleName);
             String restURL = YQLQuery.getAllLinksFromWebpage(wikipediaURL);

             List<Link> alllinks = linkExtractor.getLinks(restURL);

            // println("links:"+alllinks+alllinks.size());

                return alllinks;

                }


	public List<Place> loadPlacesViaPlacemakerFromWiki(String wikipediaArticleName) {

		String wikipediaURL = WikipediaUtils.getWikipediaArticleURL(wikipediaArticleName);
                List<Place> allplaces = loadPlacesViaPlacemakerFromURL(wikipediaURL);
                
                return allplaces;
	}

        public List<Place> loadPlacesViaPlacemakerFromURL(String URL) {


            List<Place> allPlaces = new ArrayList<Place>();

		YQLLinkExtractor linkExtractor = new YQLLinkExtractor(this);
		String[] paragraphs = linkExtractor.getMainParagraphs(URL);

		PlacemakerPlaceExtractor placeExtractor = new PlacemakerPlaceExtractor(this);
		for (int i = 0; i < paragraphs.length; i++) {
			PApplet.println("Getting places for " + i + ". p");
			List<Place> places = placeExtractor.getPlaces(paragraphs[i]);
			PApplet.println("\tplaces=" + places);
			allPlaces.addAll(places);
		}
		return allPlaces;
	}
	
}
